package xyz.example.securegeyser.command;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import xyz.example.securegeyser.SecureGeyserBridge;
import xyz.example.securegeyser.AESCipher;

import java.nio.charset.StandardCharsets;

public class SecureSendCommand implements CommandExecutor {
    private final SecureGeyserBridge plugin;

    public SecureSendCommand(SecureGeyserBridge plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 0) {
            sender.sendMessage("§c用法: /securesend <消息>");
            return true;
        }
        String msg = String.join(" ", args);
        try {
            byte[] cipher = AESCipher.encrypt(plugin.getKey(), msg.getBytes(StandardCharsets.UTF_8));
            for (Player p : Bukkit.getOnlinePlayers()) {
                p.sendPluginMessage(plugin, SecureGeyserBridge.CHANNEL, cipher);
            }
            sender.sendMessage("§a已加密发送。");
        } catch (Exception e) {
            e.printStackTrace();
            sender.sendMessage("§c发送失败: " + e.getMessage());
        }
        return true;
    }
}
